<%@page import="com.googlecode.contests_repo.core.model.Contest" %>
<%@page import="com.googlecode.contests_repo.core.model.Problem"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="com.googlecode.contests_repo.core.model.ResourcesUtils"%>
<%@page import="com.googlecode.contests_repo.core.model.DownloadUtils"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contest</title>
</head>
<body>
<%@include file='header.jsp'%>
	<%
	    String seriesTitle = request.getParameter("series");
	    String contestTitle = request.getParameter("contest");
	    String problemTitle = request.getParameter("problem");
	    String contestPath = ConfigurationManager.getInstance().getRepoRoot() + File.separator
	    + seriesTitle + File.separator + contestTitle;
	    String problemPath = contestPath + File.separator + problemTitle;
	    Problem problem = ResourcesUtils.getJSON(new File(problemPath));
	    Contest contest= ResourcesUtils.getJSON(new File(contestPath));
	    String publicFolder = "_public_files";
	    String privateFolder = "_files";
	    String loggedUser = (String)session.getAttribute("username");
	%>
	    <h1>Problem Info</h1>
		<p>
			<font size="4"> <b>Title: </b> <%=problem.getTitle()%> </font>
		</p>
		<p>
			<font size="4"> <b>About: </b> <%=problem.getAbout()%> </font>
		</p>
		<% 
			if (!ResourcesUtils.hasEnded(contest.getStartTime(),contest.getDuration())){ %>
		<p>
			<font size="4"> <b>Test weights: </b>
			<%	for(Integer testWeight: problem.getTestWeights()){ %>
				<%=testWeight%>
			<%	} %>
			</font></p>
		<%	
			}
		    if (UserManager.hasAccessRight(loggedUser, contestPath, AccessRight.VIEW_FULL)){
		%>
		<p>
			<font size="4"> <b>Notes: </b> <%=problem.getNotes()%> </font>
		</p>
		<p>
			<font size="4"> <b>Time Limit: </b> <%=problem.getTimeLimit()%> </font>
		</p>
		<p>
			<font size="4"> <b>Memory Limit: </b> <%=problem.getMemoryLimit()%> </font>
		</p>
		<p>
			<font size="4"> <b>Authors: </b>
			<%	for(String author: problem.getAuthors()){ %>
				<%=author%>
			<%	} %>
			</font></p>
		<p>
			<font size="4"> <b>Origin: </b> <%=problem.getOrigin()%> </font>
		</p>
		<%
		    }
		%>
		<h2>Download resources:</h2>
		<table border=1>
		<tr>
			<th>Resource</th>
			<th>Download</th>
		</tr>
		<%
		    List<File> downloadsList = DownloadUtils.fetchDownloadableResources(new File(
						    problemPath));
				    if (downloadsList != null)
				    {
						for (File resource : downloadsList)
						{
						    if(UserManager.hasAccessRight(loggedUser, problemPath, AccessRight.VIEW_FULL)
							    &&!resource.getName().contains("description"))
						    {
		%>
		<tr>
			<td><%=resource.getName() %></td>
			<td><a
				href="download_problem_resources.jsp?series=<%=seriesTitle%>&contest=<%=contestTitle%>&problem=<%=problem.getTitle()%>&file=<%=resource.getName()%>">download</a>
			</td>
		</tr>
		<%
		    				}
						    if(UserManager.hasAccessRight(loggedUser, problemPath, AccessRight.LIST)
							    &&resource.getName().contains("description")&&ResourcesUtils.hasStarted(contest.getStartTime()))
						    {
		%>
		<tr>
			<td><%=resource.getName() %></td>
			<td><a
				href="download_problem_resources.jsp?series=<%=seriesTitle%>&contest=<%=contestTitle%>&problem=<%=problem.getTitle()%>&file=<%=resource.getName()%>">download</a>
			</td>
		</tr>
		<%
		    				}
		    			}
		    		}
		if (new File(problemPath + File.separator + publicFolder).exists()
				&&ResourcesUtils.hasStarted(contest.getStartTime())){%>
			<tr>
				<td><%=publicFolder%></td>
				<td><a
						href="download_folder.jsp?from=problem&series=<%=seriesTitle%>&contest=<%=contestTitle%>&problem=<%=problem.getTitle()%>&folder=<%=publicFolder%>">download</a>
				</td>
			</tr>
		<%
			}
			if (new File(problemPath + File.separator + privateFolder).exists()
					&&UserManager.hasAccessRight(loggedUser, contestPath,AccessRight.VIEW_FULL))
			{%>
			<tr>
				<td><%=privateFolder%></td>
				<td><a
						href="download_folder.jsp?from=problem&series=<%=seriesTitle%>&contest=<%=contestTitle%>&problem=<%=problem.getTitle()%>&folder=<%=privateFolder%>">download</a>
				</td>
			</tr>
			<%
				}
			%>
	</table>
	<br />
	<br />
<a href="contest.jsp?series=<%=seriesTitle%>&contest=<%=contestTitle%>">Back</a>
</body>
</html>